<?php

abstract class Core_Model_DbTable_Base_Buffer extends MKT_Model_DbTable_Base {

//    public function getEntriesForUpdating($count) {
//        $expired_time = Core_Util::getExpiredUpdatingTime();
//        try {
//            $this->getAdapter()->beginTransaction();
//            $sql = $this->select()
//                    ->forUpdate()
//                    ->from($this->_name, array("{$this->_id_social_name} AS id"))
//                    ->where (
//                            "(update_status = " . NOT_UPDATED . ") OR " .
//                            "(update_status = " . UPDATING . " AND start_update_time <= $expired_time)"
//                    )
//                    ->limit($count);
//            $rows = $this->fetchAll($sql);
//            $this->seToUpdating($rows);
//            $this->getAdapter()->commit();
//        } catch (Exception $e) {
//            $log = Zend_Registry::get("log");
//            $log->log("{$e->getMessage()} when get posts for updating", 0);
//        }
//        return $rows;
//    }

    public function getEntriesForUpdating($id_social_page, $end_time, $count) {
        $expired_time = Core_Util::getExpiredUpdatingTime();
        try {
            $this->getAdapter()->beginTransaction();
            $sql = $this->select()
                    ->forUpdate()
                    ->from($this->_name, array("{$this->_id_social_name} AS id"))
                    ->where("id_social_page = '$id_social_page'")
                    ->where("created_time < $end_time")
                    ->where(
                            "(update_status = " . NOT_UPDATED . ") OR " .
                            "(update_status = " . UPDATING . " AND start_update_time <= $expired_time)"
                    )
                    ->limit($count);
            $rows = $this->fetchAll($sql);
            $this->seToUpdating($rows);
            $this->getAdapter()->commit();
        } catch (Exception $e) {
            $log = Zend_Registry::get("log");
            $log->log("{$e->getMessage()} when get posts for updating", 0);
        }
        return $rows;
    }
    
    public function hasFinishUpdated($id_social_page, $end_time) {
        $query = $this->select()
                      ->from($this->_name, array("count" => "COUNT(*)"))
                      ->where("id_social_page = '$id_social_page'")
                      ->where("created_time < $end_time");
        $count = $this->fetchRow($query);
        return $count->count == 0;
    }
    
    protected function seToUpdating($rows) {
        if (!empty($rows) && $rows->count() > 0) {
            $time = time();
            $id_socials = array();
            $primary_key = $this->_primary[1];
            foreach ($rows as $row) {
                $id_socials[] = $row->$primary_key;
            }
            $cond = $this->getAdapter()->quoteinto("{$this->_id_social_name} IN (?)", $id_socials);

            $this->update(array (
                "update_status"     => UPDATING,
                'start_update_time' => $time
            ), $cond);
        }
    }
    
    public function deleteBatch($id_socials) {
        $cond = $this->getAdapter()->quoteInto("{$this->_id_social_name} IN (?)", $id_socials);
        $this->delete($cond);
    }   
}

?>
